package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.vo.DyAnchorVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.DyVipSettingBo;
import com.ruoyi.system.domain.vo.DyVipSettingVo;
import com.ruoyi.system.domain.DyVipSetting;
import com.ruoyi.system.mapper.DyVipSettingMapper;
import com.ruoyi.system.service.IDyVipSettingService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-26
 */
@RequiredArgsConstructor
@Service
public class DyVipSettingServiceImpl implements IDyVipSettingService {

    private final DyVipSettingMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public DyVipSettingVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<DyVipSettingVo> queryPageList(DyVipSettingBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DyVipSetting> lqw = buildQueryWrapper(bo);
        Page<DyVipSettingVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<DyVipSettingVo> queryList(DyVipSettingBo bo) {
        LambdaQueryWrapper<DyVipSetting> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DyVipSetting> buildQueryWrapper(DyVipSettingBo bo) {
        LambdaQueryWrapper<DyVipSetting> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getVipName()), DyVipSetting::getVipName, bo.getVipName());
        lqw.eq(bo.getMoney() != null, DyVipSetting::getMoney, bo.getMoney());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(DyVipSettingBo bo) {
        DyVipSetting add = BeanUtil.toBean(bo, DyVipSetting.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(DyVipSettingBo bo) {
        DyVipSetting update = BeanUtil.toBean(bo, DyVipSetting.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DyVipSetting entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<DyVipSettingVo> getVipSettingPage(DyVipSettingBo bo, PageQuery pageQuery) {
        Page<DyVipSettingVo> result = baseMapper.selectVoPage(pageQuery.build(), new LambdaQueryWrapper<DyVipSetting>().orderByDesc(DyVipSetting::getCreateTime));
        return TableDataInfo.build(result);
    }

    @Override
    public List<DyVipSettingVo> getVipSettingList(DyVipSettingBo bo) {
        return baseMapper.selectVoList(new LambdaQueryWrapper<DyVipSetting>().orderByDesc(DyVipSetting::getCreateTime));
    }

    @Override
    public int addDyVipSetting(DyVipSettingBo bo) {

        if (StringUtils.isEmpty(bo.getVipName()) || bo.getMoney() == null){
            throw new ServiceException("需要添加的数据不全");
        }

        List<DyVipSetting> settings = baseMapper.selectList(new LambdaQueryWrapper<DyVipSetting>().eq(DyVipSetting::getVipName, bo.getVipName()));
        if (CollUtil.isNotEmpty(settings)){
            throw new ServiceException("vip名称已经存在");
        }

        DyVipSetting setting = BeanUtil.toBean(bo, DyVipSetting.class);
        setting.setCreateTime(new Date());
        setting.setUpdateTime(new Date());

        return baseMapper.insert(setting);
    }

    @Override
    public int updDyVipSetting(DyVipSettingBo bo) {

        if (StringUtils.isEmpty(bo.getVipName()) || bo.getMoney() == null || bo.getId() == null){
            throw new ServiceException("需要修改的数据不全");
        }

        List<DyVipSetting> settings = baseMapper.selectList(new LambdaQueryWrapper<DyVipSetting>()
                .eq(DyVipSetting::getVipName, bo.getVipName())
                .ne(DyVipSetting::getId, bo.getId()));

        if (CollUtil.isNotEmpty(settings)){
            throw new ServiceException("vip名称已经存在");
        }

        DyVipSetting setting = BeanUtil.toBean(bo, DyVipSetting.class);
        setting.setUpdateTime(new Date());

        return baseMapper.updateById(setting);
    }

    @Override
    public int delDyVipSetting(DyVipSettingBo bo) {

        if (bo.getId() == null){
            throw new ServiceException("需要删除的数据不全");
        }

        return baseMapper.deleteById(bo.getId());
    }
}
